package com.github.prontera.controller;

import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.AppBanner;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.ActivityService;
import com.github.prontera.service.AppBannerService;
import com.github.prontera.util.Page;


/**
 * banner
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX + "/banner", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class AppBannerController{

	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private AppBannerService appBannerService;
	
	/**
	 * 查询banner列表
	 */
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查询banner列表" , notes = "查询活动列表")
	public ObjectDataResponse<Page<AppBanner>> find(@RequestBody AppBanner appBanner){
		
		appBanner.setOrderBy("APP_BANNER.create_time desc");
		Page<AppBanner> datas =appBannerService.page(appBanner);
		return new ObjectDataResponse<Page<AppBanner>>(datas);
	}
	
	
	/**
	 * 删除banner
	 */
	@ApiOperation(value = "删除banner", notes = "删除banner")
	@RequestMapping(value = "/deleteBanner", method = RequestMethod.POST)
	@ResponseBody
	public Response deleteActivity(@CurrentUserId Long userId, @RequestBody AppBanner appBanner) {
		int success = appBannerService.remove(appBanner);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "删除banner失败！");	
		}
		return new ObjectDataResponse<Object>(map);
	}
	

	/**
	 * 修改banner
	 */
	@ApiOperation(value = "修改banner", notes = "修改banner")
	@RequestMapping(value = "updateBanner", method = RequestMethod.POST)
	@ResponseBody
	public Response updateActivity(@CurrentUserId Long userId,@RequestBody AppBanner appBanner) {
		int success = appBannerService.update(appBanner);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "修改banner失败！");
		}
		return new ObjectDataResponse<Object>(map);
	}
	
	/**
	 * 添加banner
	 */
	@ApiOperation(value = "添加banner", notes = "添加banner")
	@RequestMapping(value = "addBanner", method = RequestMethod.POST)
	@ResponseBody
	public Response addDict(@RequestBody AppBanner appBanner) {
		int success = appBannerService.insert(appBanner);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", success);
		if(success == 2){
			map.put("msg", "新增banner失败！");
		}
		return new ObjectDataResponse<Object>(map);
	}
	
}
